package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.ProductIn;
import com.tyqx.hashchainbiz.service.ProductInService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.ProductInMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import com.tyqx.hashchainutil.common.mongodb.ImagePathHashValueUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("productInService")
public class ProductInServiceImpl implements ProductInService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private ProductInMapper productInMapper;

    @Async
    public Integer productIn(ProductIn productIn)throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        productIn.setCreateTime(createTime);
        productIn.setUpdateTime(createTime);

        String productInStr= productIn.toString();

        String attachmentStr=productIn.getAttachment();
        if (!"".equals(attachmentStr) && attachmentStr != null) {
            String collectionName = "productIn";
            String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
            productIn.setAttachment(imageHashValue);
        }
        HashIndex hashIndex=HashValueUtil.getHashValue(productInStr,mongoTemplate);
        Integer flag=hashIndexMapper.insert(hashIndex);
        productIn.setHashValue(hashIndex.getHashValue());
        return productInMapper.addInProduct(productIn);
    }

    @Override
    public List<ProductIn> getProductInById(String donateId, String userName) {
        return productInMapper.getProductInById(donateId);
    }

    @Override
    public List<ProductIn> getProductInByTime(Date startTime, Date endTime, String userName) {
        return productInMapper.getProductInByTime(startTime, endTime, userName);
    }


}
